package com.way.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@Slf4j
public class RestTemplateUtil {

    @Autowired
    private RestTemplate restTemplate;

    public String sendPost(String url,String params) {
        log.info("sendPost url: {}",url);
        log.info("sendPost params: {}",params);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> httpEntity = new HttpEntity<>(params, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        log.info("sendPost responseEntity: {}",responseEntity);
        return responseEntity.getBody();
    }

    public String sendGet(String url) {
        log.info("sendGet url: {}",url);
        String response = restTemplate.getForObject(url, String.class);
        log.info("response: {}",response);
        return response;
    }
}
